<!doctype html><html lang dir=ltr><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>【第3周笔记】基本数据类型 | Python | Hui.Ke - Blog</title><meta name=generator content="Hugo Eureka 0.9.3"><link rel=stylesheet href=https://b.hui.ke/css/eureka.min.9cec6350e37e534b0338fa9a085bf06855de3b0f2dcf857e792e5e97b07ea905d4d5513db554cbc26a9c3da622bae92d.css><script defer src=https://b.hui.ke/js/eureka.min.fa9a6bf6d7a50bb635b4cca7d2ba5cf3dfb095ae3798773f1328f7950028b48c17d06276594e1b5f244a25a6c969a705.js></script>
<link rel=preconnect href=https://fonts.googleapis.com><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link rel=preload href="https://fonts.googleapis.com/css2?family=Lora:wght@400;600;700&family=Noto+Serif+SC:wght@400;600;700&display=swap" as=style onload='this.onload=null,this.rel="stylesheet"'><link rel=stylesheet href=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/styles/vs.min.css media=print onload='this.media="all",this.onload=null' crossorigin><script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/bash.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/ini.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/json.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/php.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/python.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/shell.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/sql.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/x86asm.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/xml.min.js crossorigin></script>
<link rel=stylesheet href=https://b.hui.ke/css/highlightjs.min.2958991528e43eb6fc9b8c4f2b8e052f79c4010718e1d1e888a777620e9ee63021c2c57ec7417a3108019bb8c41943e6.css media=print onload='this.media="all",this.onload=null'><script defer type=text/javascript src=https://lib.baomitu.com/font-awesome/6.1.1/js/all.min.js></script>
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.css integrity=sha384-MlJdn/WNKDGXveldHDdyRP1R4CTHr3FeuDNfhsLPYrq2t0UBkUdK2jyTnXPEK1NQ media=print onload='this.media="all",this.onload=null' crossorigin><script defer src=https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.js integrity=sha384-VQ8d8WVFw0yHhCk5E8I86oOhv48xLpnDZx5T9GogA/Y84DcCKWXDmSDfn13bzFZY crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/contrib/auto-render.min.js integrity=sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR crossorigin></script>
<script>document.addEventListener("DOMContentLoaded",function(){renderMathInElement(document.body,{delimiters:[{left:"$$",right:"$$",display:!0},{left:"$",right:"$",display:!1},{left:"\\(",right:"\\)",display:!1},{left:"\\[",right:"\\]",display:!0}]})})</script><script defer src=https://cdn.jsdelivr.net/npm/mermaid@8.14.0/dist/mermaid.min.js integrity=sha384-atOyb0FxAgN9LyAc6PEf9BjgwLISyansgdH8/VXQH8p2o5vfrRgmGIJ2Sg22L0A0 crossorigin></script>
<link rel=icon type=image/png sizes=32x32 href=https://b.hui.ke/icon_hub7ca0b5404c6d576559b2bd22c64b0e5_2009_32x32_fill_box_center_3.png><link rel=apple-touch-icon sizes=180x180 href=https://b.hui.ke/icon_hub7ca0b5404c6d576559b2bd22c64b0e5_2009_180x180_fill_box_center_3.png><meta name=description content="  课程笔记和选择题"><script type=application/ld+json>{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Docs","item":"https://b.hui.ke/docs/"},{"@type":"ListItem","position":2,"name":"Python","item":"https://b.hui.ke/docs/python/"},{"@type":"ListItem","position":3,"name":"【第3周笔记】基本数据类型","item":"https://b.hui.ke/docs/python/3/"}]}</script><script type=application/ld+json>{"@context":"https://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://b.hui.ke/docs/python/3/"},"headline":"【第3周笔记】基本数据类型 | Python | Hui.Ke - Blog","datePublished":"2021-01-04T16:41:50+08:00","dateModified":"2022-04-07T08:57:50+08:00","wordCount":2516,"author":{"@type":"Person","name":"Hui.Ke"},"publisher":{"@type":"Person","name":"Hui.Ke","logo":{"@type":"ImageObject","url":"https://b.hui.ke/icon.png"}},"description":"\u003cp\u003e  课程笔记和选择题\u003c\/p\u003e"}</script><meta property="og:title" content="【第3周笔记】基本数据类型 | Python | Hui.Ke - Blog"><meta property="og:type" content="article"><meta property="og:image" content="https://b.hui.ke/icon.png"><meta property="og:url" content="https://b.hui.ke/docs/python/3/"><meta property="og:description" content="  课程笔记和选择题"><meta property="og:site_name" content="Hui.Ke - Blog"><meta property="article:published_time" content="2021-01-04T16:41:50+08:00"><meta property="article:modified_time" content="2022-04-07T08:57:50+08:00"><meta property="article:section" content="docs"><meta property="article:tag" content="数据类型"><meta property="article:tag" content="time"><meta property="article:tag" content="文本进度条"><meta property="og:see_also" content="https://b.hui.ke/docs/python/20/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/10/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/19/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/9/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/18/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/8/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/17/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/7/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/16/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/6/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/15/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/5/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/14/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/4/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/13/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/12/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/2/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/11/"><meta property="og:see_also" content="https://b.hui.ke/docs/python/1/"><script>!function(e){"use strict";!function(){var i,s=window,o=document,a=e,c="".concat("https:"===o.location.protocol?"https://":"http://","sdk.51.la/js-sdk-pro.min.js"),n=o.createElement("script"),r=o.getElementsByTagName("script")[0];n.type="text/javascript",n.setAttribute("charset","UTF-8"),n.async=!0,n.src=c,n.id="LA_COLLECT",a.d=n,i=function(){s.LA.ids.push(a)},s.LA?s.LA.ids&&i():(s.LA=e,s.LA.ids=[],i()),r.parentNode.insertBefore(n,r)}()}({id:"Jgb8aUbG5e3rqhrs",ck:"Jgb8aUbG5e3rqhrs",autoTrack:!0,hashMode:!0})</script><body class="flex min-h-screen flex-col"><header class="min-h-16 pl-scrollbar bg-secondary-bg fixed z-50 flex w-full items-center shadow-sm"><div class="mx-auto w-full max-w-screen-xl"><script>let storageColorScheme=localStorage.getItem("lightDarkMode");((storageColorScheme=="Auto"||storageColorScheme==null)&&window.matchMedia("(prefers-color-scheme: dark)").matches||storageColorScheme=="Dark")&&document.getElementsByTagName("html")[0].classList.add("dark")</script><nav class="flex items-center justify-between flex-wrap px-4 py-4 md:py-0"><a href=/ class="me-6 text-primary-text text-xl font-bold">Hui.Ke - Blog</a>
<button id=navbar-btn class="md:hidden flex items-center px-3 py-2" aria-label="Open Navbar">
<i class="fas fa-bars"></i></button><div id=target class="hidden block md:flex md:grow md:justify-between md:items-center w-full md:w-auto text-primary-text z-20"><div class="md:flex md:h-16 text-sm md:grow pb-4 md:pb-0 border-b md:border-b-0"><a href=/posts/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Posts</a>
<a href=/docs/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 selected-menu-item me-4">Docs</a>
<a href=/categories/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Categories</a>
<a href=/series/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Series</a>
<a href=/tags/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Tags</a>
<a href=/love/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Love</a>
<a href=/about/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">About</a></div><div class=flex><div class="relative pt-4 md:pt-0"><div class="cursor-pointer hover:text-eureka" id=lightDarkMode><i class="fas fa-adjust"></i></div><div class="fixed hidden inset-0 opacity-0 h-full w-full cursor-default z-30" id=is-open></div><div class="absolute flex flex-col start-0 md:start-auto end-auto md:end-0 hidden bg-secondary-bg w-48 rounded py-2 border border-tertiary-bg cursor-pointer z-40" id=lightDarkOptions><span class="px-4 py-1 hover:text-eureka" name=Light>Light</span>
<span class="px-4 py-1 hover:text-eureka" name=Dark>Dark</span>
<span class="px-4 py-1 hover:text-eureka" name=Auto>Auto</span></div></div></div></div><div class="fixed hidden inset-0 opacity-0 h-full w-full cursor-default z-0" id=is-open-mobile></div></nav><script>let element=document.getElementById("lightDarkMode");storageColorScheme==null||storageColorScheme=="Auto"?document.addEventListener("DOMContentLoaded",()=>{window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",switchDarkMode)}):storageColorScheme=="Light"?(element.firstElementChild.classList.remove("fa-adjust"),element.firstElementChild.setAttribute("data-icon","sun"),element.firstElementChild.classList.add("fa-sun")):storageColorScheme=="Dark"&&(element.firstElementChild.classList.remove("fa-adjust"),element.firstElementChild.setAttribute("data-icon","moon"),element.firstElementChild.classList.add("fa-moon")),document.addEventListener("DOMContentLoaded",()=>{getcolorscheme(),switchBurger()})</script></div></header><main class="grow pt-16"><div class=pl-scrollbar><div class="mx-auto w-full max-w-screen-xl lg:px-4 xl:px-8"><div class=lg:pt-12><div class="flex flex-col md:flex-row bg-secondary-bg rounded"><div class="md:w-1/4 lg:w-1/5 border-e"><div class="sticky top-16 pt-6"><div id=sidebar-title class="md:hidden mx-4 px-2 pt-4 pb-2 md:border-b text-tertiary-text md:text-primary-text"><span class=font-semibold>Table of Contents</span>
<i class='fas fa-caret-right ms-1'></i></div><div id=sidebar-toc class="hidden md:block overflow-y-auto mx-6 md:mx-0 pe-6 pt-2 md:max-h-doc-sidebar bg-primary-bg md:bg-transparent"><div class="flex flex-wrap ms-4 -me-2 p-2 bg-secondary-bg md:bg-primary-bg rounded"><a class=hover:text-eureka href=https://b.hui.ke/docs/python/>Python</a></div><ul class=ps-6><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/20/>期末测验</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/10/>【第9+周笔记】全课程总结与学习展望</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/19/>【第9周程序】计算生态纵览</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/9/>【第9周笔记】计算生态纵览</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/18/>【第8周程序】程序设计方法学</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/8/>【第8周笔记】程序设计方法学</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/17/>【第7周程序】文件和数据格式化</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/7/>【第7周笔记】文件和数据格式化</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/16/>【第6周程序】组合数据类型</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/6/>【第6周笔记】组合数据类型</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/15/>【第5周程序】函数和代码复用</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/5/>【第5周笔记】函数和代码复用</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/14/>【第4周程序】程序的控制结构</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/4/>【第4周笔记】程序的控制结构</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/13/>【第3周程序】基本数据类型</a></div></li><li class=py-2><div><a class="text-eureka hover:text-eureka" href=https://b.hui.ke/docs/python/3/>【第3周笔记】基本数据类型</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/12/>【第2周程序】Python 基本图形绘制</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/2/>【第2周笔记】Python 基本图形绘制</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/11/>【第1周程序】Python 基本语法元素</a></div></li><li class=py-2><div><a class=hover:text-eureka href=https://b.hui.ke/docs/python/1/>【第1周笔记】Python 基本语法元素</a></div></li></ul></div></div></div><div class="w-full md:w-3/4 lg:w-4/5 pb-8 pt-2 md:pt-8"><div class=flex><div class="w-full lg:w-3/4 px-6"><article class=prose><h1 class=mb-4>【第3周笔记】基本数据类型</h1><div class="text-tertiary-text not-prose mt-2 flex flex-row flex-wrap items-center"><div class="me-6 my-2"><i class="fas fa-calendar me-1"></i>
<span>2021-01-04</span></div><div class="me-6 my-2"><i class="fa-solid fa-pen-to-square me-1"></i>
<span>2022-04-07</span></div><div class="me-6 my-2"><i class="fas fa-clock me-1"></i>
<span>6 min read</span></div><div class="me-6 my-2"><i class="fas fa-folder me-1"></i>
<a href=https://b.hui.ke/categories/it/ class=hover:text-eureka>IT</a></div><div class="me-6 my-2"><i class="fas fa-th-list me-1"></i>
<a href=https://b.hui.ke/series/python/ class=hover:text-eureka>Python</a></div><div class="me-6 my-2"><i class="fa-solid fa-eye me-1"></i>
<span id=busuanzi_value_page_pv><i class="fa fa-spinner fa-spin"></i></span> Hits</div></div><p>  课程笔记和选择题</p><h2 id=notes>Notes</h2><h3 id=整数类型>整数类型</h3><p>  与数学中整数的概念一致，可正可负，<strong>没有取值范围限制</strong></p><p>  pow(x,y) 函数：计算 $ x^y $ ，想算多大算多大</p><table><thead><tr><th style=text-align:left>四种进制</th><th style=text-align:left>表现形式</th><th style=text-align:left>举例</th></tr></thead><tbody><tr><td style=text-align:left>十进制</td><td style=text-align:left>直接表示</td><td style=text-align:left>1010，99，-217</td></tr><tr><td style=text-align:left>二进制</td><td style=text-align:left>以 0b 或 0B 开头</td><td style=text-align:left>0b010，-0B101</td></tr><tr><td style=text-align:left>八进制</td><td style=text-align:left>以 0o 或 0O 开头</td><td style=text-align:left>0o123，-0O456</td></tr><tr><td style=text-align:left>十六进制</td><td style=text-align:left>以 0x 或 0X 开头</td><td style=text-align:left>0x9a，-0X89</td></tr></tbody></table><h3 id=浮点数类型>浮点数类型</h3><p>  取值范围数量级约 $ -10^{307} $ 到 $ 10^{308} $ ，精度数量级 $ 10^{-16} $</p><blockquote><p>注意：浮点数间运算存在不确定尾数，不是 bug</p></blockquote><p>  round(x, d)：对 x 四舍五入，d 是小数截取位数</p><p>  浮点数间运算与比较用 round() 函数辅助</p><p>  不确定尾数一般发生在 $ 10^{-16} $ 左右，round() 十分有效</p><p>  浮点数可以采用科学计数法表示，如 aeb：表示 $ a×10^b $</p><p>  例如：4.3e-3 值为 0.0043，9.6E5 值为 960000.0</p><h3 id=复数类型>复数类型</h3><p>  复数实例：z = 1.23e-4+5.6e+89j，z.real 获得实部，z.imag 获得虚部</p><h3 id=数字类型的关系>数字类型的关系</h3><p>  类型间可进行混合运算，生成结果为“最宽”类型，三种类型存在一种逐渐“扩展”或“变宽”的关系：整数 → 浮点数 → 复数，例如：123+4.0=127.0（整数+浮点数=浮点数)</p><table><thead><tr><th style=text-align:left>数值运算函数</th><th style=text-align:left>描述</th><th style=text-align:left>举例</th><th style=text-align:left>结果</th></tr></thead><tbody><tr><td style=text-align:left>abs(x)</td><td style=text-align:left>绝对值，x 的绝对值</td><td style=text-align:left>abs(-10.01)</td><td style=text-align:left>10.01</td></tr><tr><td style=text-align:left>divmod(x,y)</td><td style=text-align:left>商余（x//y，x%y）同时输出商和余数</td><td style=text-align:left>divmod(10, 3)</td><td style=text-align:left>(3, 1)</td></tr><tr><td style=text-align:left>pow(x, y[, z])</td><td style=text-align:left>幂余，(x**y)%z，参数 z 可省略</td><td style=text-align:left>pow(3, pow(3, 99), 10000)</td><td style=text-align:left>4587</td></tr><tr><td style=text-align:left>round(x[, d])</td><td style=text-align:left>四舍五入，d 是保留小数位数，默认值为 0</td><td style=text-align:left>round(-10.123, 2)</td><td style=text-align:left>-10.12</td></tr><tr><td style=text-align:left>max( $ x_1,x_2, … ,x_n $ )</td><td style=text-align:left>最大值，返回 $ x_1,x_2, … ,x_n $ 中的最大值，n 不限</td><td style=text-align:left>max(1, 9, 5, 4, 3)</td><td style=text-align:left>9</td></tr><tr><td style=text-align:left>min( $ x_1,x_2, … ,x_n $ )</td><td style=text-align:left>最小值，返回 $ x_1,x_2, … ,x_n $ 中的最小值，n 不限</td><td style=text-align:left>min(1, 9, 5, 4, 3)</td><td style=text-align:left>1</td></tr><tr><td style=text-align:left>int(x)</td><td style=text-align:left>将 x 变成整数，舍弃小数部分</td><td style=text-align:left>int(123.45) 或 int(&ldquo;123&rdquo;)</td><td style=text-align:left>123</td></tr><tr><td style=text-align:left>float(x)</td><td style=text-align:left>将 x 变成浮点数，增加小数部分</td><td style=text-align:left>float(12)</td><td style=text-align:left>12.0</td></tr><tr><td style=text-align:left>complex(x)</td><td style=text-align:left>将 x 变成复数，增加虚数部分</td><td style=text-align:left>complex(4)</td><td style=text-align:left>4 + 0j</td></tr></tbody></table><h3 id=字符串>字符串</h3><p>  字符串有两类共四种表示方法：一对单引号、一对双引号、一对单双引号、一对三单或三双引号（表示多行字符串）</p><p>  x in s：如果 x 是 s 的子串，返回 True，否则返回 False</p><table><thead><tr><th style=text-align:left>字符串处理函数</th><th style=text-align:left>描述</th><th style=text-align:left>举例</th></tr></thead><tbody><tr><td style=text-align:left>len(x)</td><td style=text-align:left>长度，返回字符串 x 的长度</td><td style=text-align:left>len(&ldquo;一二三456&rdquo;) 结果为 6</td></tr><tr><td style=text-align:left>str(x)</td><td style=text-align:left>任意类型 x 所对应的字符串形式</td><td style=text-align:left>str(1.23) 结果为 &ldquo;1.23&rdquo;，str([1,2]) 结果为 &ldquo;[1,2]&rdquo;</td></tr><tr><td style=text-align:left>hex(x) 或 oct(x)</td><td style=text-align:left>整数x的十六进制或八进制<strong>小写</strong>形式字符串</td><td style=text-align:left>hex(425) 结果为 &ldquo;0x1a9&rdquo;，oct(425) 结果为 &ldquo;0o651&rdquo;</td></tr><tr><td style=text-align:left>chr(u)</td><td style=text-align:left>x 为 Unicode 编码，返回其对应的字符</td><td></td></tr><tr><td style=text-align:left>ord(x)</td><td style=text-align:left>x 为字符，返回其对应的 Unicode 编码</td><td></td></tr></tbody></table><table><thead><tr><th style=text-align:left>字符串处理方法</th><th style=text-align:left>描述</th></tr></thead><tbody><tr><td style=text-align:left>str.lower() 或 str.upper()</td><td style=text-align:left>返回字符串的副本，全部字符小写/大写</td></tr><tr><td style=text-align:left>str.split(sep=None)</td><td style=text-align:left>返回一个列表，由 str 根据 sep 被分隔的部分组成</td></tr><tr><td style=text-align:left>str.count(sub)</td><td style=text-align:left>返回子串 sub 在 str 中出现的次数</td></tr><tr><td style=text-align:left>str.replace(old, new)</td><td style=text-align:left>返回字符串 str 副本，所有 old 子串被替换为 new</td></tr><tr><td style=text-align:left>str.center(width[,fillchar])</td><td style=text-align:left>字符串 str 根据宽度 width 居中，fillchar 可选</td></tr><tr><td style=text-align:left>str.strip(chars)</td><td style=text-align:left>从 str 中去掉在其左侧和右侧 chars 中列出的字符</td></tr><tr><td style=text-align:left>str.join(iter)</td><td style=text-align:left>在 iter 变量除最后元素外每个元素后增加一个 str</td></tr></tbody></table><table><thead><tr><th style=text-align:left>举例</th><th style=text-align:left>结果</th></tr></thead><tbody><tr><td style=text-align:left>&ldquo;AbCdEfGh&rdquo;.lower()</td><td style=text-align:left>&ldquo;abcdefgh&rdquo;</td></tr><tr><td style=text-align:left>&ldquo;A,B,C&rdquo;.split(",")</td><td style=text-align:left>[&lsquo;A&rsquo;,&lsquo;B&rsquo;,&lsquo;C&rsquo;]</td></tr><tr><td style=text-align:left>&ldquo;an apple a day&rdquo;.count(&ldquo;a&rdquo;)</td><td style=text-align:left>4</td></tr><tr><td style=text-align:left>&ldquo;python&rdquo;.replace(&ldquo;n&rdquo;,&ldquo;n123.io&rdquo;)</td><td style=text-align:left>&ldquo;python123.io&rdquo;</td></tr><tr><td style=text-align:left>&ldquo;python&rdquo;.center(20,"=")</td><td style=text-align:left>&lsquo;=======python=======&rsquo;</td></tr><tr><td style=text-align:left>&ldquo;= python= &ldquo;.strip(&rdquo; =np&rdquo;)</td><td style=text-align:left>&ldquo;ytho&rdquo;</td></tr><tr><td style=text-align:left>&ldquo;,".join(&ldquo;12345&rdquo;)</td><td style=text-align:left>&ldquo;1,2,3,4,5&rdquo; #主要用于字符串分隔等</td></tr></tbody></table><h3 id=unicode-编码>Unicode 编码</h3><p>  统一字符编码，即覆盖几乎所有字符的编码方式</p><p>  从 0 到 1114111（0x10FFFF）空间，每个编码对应一个字符</p><p>  Python 字符串中每个字符都是 Unicode 编码字符</p><pre><code class=language-python>for i in range(1000):
    print(chr(9800+i),end=&quot;&quot;)     # 输出从星座（9800 是白羊）开始的特殊符号
</code></pre><div text-align:center><img src=/images/format方法的格式控制0.webp></a>
<img src=/images/format方法的格式控制1.webp></a>
<img src=/images/format方法的格式控制2.webp></a></div><h3 id=time-库>time 库</h3><p>time 库是 Python 中处理时间的标准库</p><h4 id=时间获取>时间获取</h4><table><thead><tr><th style=text-align:left>时间获取函数</th><th style=text-align:left>描述</th><th style=text-align:left>用法</th><th style=text-align:left>结果</th></tr></thead><tbody><tr><td style=text-align:left>time()</td><td style=text-align:left>获取当前时间戳，即计算机内部时间值，浮点数</td><td style=text-align:left>time.time()</td><td style=text-align:left>1516939876.6022282</td></tr><tr><td style=text-align:left>ctime()</td><td style=text-align:left>获取当前时间并以易读方式表示，返回字符串</td><td style=text-align:left>time.ctime()</td><td style=text-align:left>&lsquo;Fri Jan 26 12:11:16 2018&rsquo;</td></tr><tr><td style=text-align:left>gmtime()</td><td style=text-align:left>获取当前时间，表示为计算机可处理的时间格式</td><td style=text-align:left>time.gmtime()</td><td style=text-align:left>time.struct_time(tm_year=2018, tm_mon=1,……, tm_yday=26, tm_isdst=0)</td></tr></tbody></table><h4 id=时间格式化函数>时间格式化函数</h4><ol><li>strftime(tpl, ts)<ol><li>tpl 是格式化模板字符串，用来定义输出效果</li><li>ts 是计算机内部时间类型变量<pre><code class=language-python>&gt;&gt;&gt;t = time.gmtime()
&gt;&gt;&gt;time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;,t)
'2018-01-26 12:55:20'
</code></pre></li></ol></li><li>strptime(str, tpl)<ol><li>str 是字符串形式的时间值</li><li>tpl 是格式化模板字符串，用来定义输入效果<pre><code class=language-python>&gt;&gt;&gt;timeStr = '2018-01-26 12:55:20'
&gt;&gt;&gt;time.strptime(timeStr, &quot;%Y-%m-%d %H:%M:%S&quot;)
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=26, tm_hour=4, tm_min=11, tm_sec=16, tm_wday=4, tm_yday=26, tm_isdst=0)
</code></pre></li></ol></li></ol><div text-align:center><img src=/images/时间格式化.webp></a></div><table><thead><tr><th style=text-align:left>格式化控制符</th><th style=text-align:left>日期/时间说明</th><th style=text-align:left>值范围</th><th style=text-align:left>实例</th></tr></thead><tbody><tr><td style=text-align:left>%Y</td><td style=text-align:left>年份</td><td style=text-align:left>0000~9999</td><td style=text-align:left>1900</td></tr><tr><td style=text-align:left>%m</td><td style=text-align:left>月份</td><td style=text-align:left>01~12</td><td style=text-align:left>10</td></tr><tr><td style=text-align:left>%B</td><td style=text-align:left>月份名称</td><td style=text-align:left>January~December</td><td style=text-align:left>April</td></tr><tr><td style=text-align:left>%b</td><td style=text-align:left>月份名称缩写</td><td style=text-align:left>Jan~Dec</td><td style=text-align:left>Apr</td></tr><tr><td style=text-align:left>%d</td><td style=text-align:left>日期</td><td style=text-align:left>01~31</td><td style=text-align:left>25</td></tr><tr><td style=text-align:left>%A</td><td style=text-align:left>星期</td><td style=text-align:left>Monday~Sunday</td><td style=text-align:left>Wednesday</td></tr><tr><td style=text-align:left>%a</td><td style=text-align:left>星期缩写</td><td style=text-align:left>Mon~Sun</td><td style=text-align:left>Wed</td></tr><tr><td style=text-align:left>%H</td><td style=text-align:left>小时（24h 制）</td><td style=text-align:left>00~23</td><td style=text-align:left>12</td></tr><tr><td style=text-align:left>%I</td><td style=text-align:left>小时（12h 制）</td><td style=text-align:left>01~12</td><td style=text-align:left>7</td></tr><tr><td style=text-align:left>%p</td><td style=text-align:left>上/下午</td><td style=text-align:left>AM, PM</td><td style=text-align:left>PM</td></tr><tr><td style=text-align:left>%M</td><td style=text-align:left>分钟</td><td style=text-align:left>00~59</td><td style=text-align:left>26</td></tr><tr><td style=text-align:left>%S</td><td style=text-align:left>秒</td><td style=text-align:left>00~59</td><td style=text-align:left>26</td></tr></tbody></table><h4 id=程序计时>程序计时</h4><ol><li>perf_counter()<ol><li>返回一个 CPU 级别的精确时间计数值，单位为秒</li><li>由于这个计数值起点不确定，<strong>连续调用差值才有意义</strong></li></ol></li></ol><pre><code class=language-python>&gt;&gt;&gt;start = time.perf_counter()
318.66599499718114
&gt;&gt;&gt;end = time.perf_counter()
341.3905185375658
&gt;&gt;&gt;end - start
22.724523540384666
</code></pre><ol start=2><li>sleep(s)：s 拟休眠的时间，单位是秒，可以是浮点数</li></ol><pre><code class=language-python>&gt;&gt;&gt;def wait():
time.sleep(3.3)
&gt;&gt;&gt;wait()     # 程序将等待 3.3 秒后再退出
</code></pre><h2 id=测验-3>测验 3</h2><ol><li><p>pow(x, 0.5) 能够计算 x 的平方根，计算负数的平方根将产生：D</p><p>A. 无输出</p><p>B. ValueError错误</p><p>C. 程序崩溃</p><p>D. 复数</p></li></ol><pre><code class=language-python>&gt;&gt;&gt; pow(-1, 0.5)
(6.123233995736766e-17+1j)
</code></pre><ol start=2><li><p>以下关于字符串 .strip() 方法功能说明正确的是：D</p><p>A. 替换字符串中特定字符</p><p>B. 连接两个字符串序列</p><p>C. 按照指定字符分割字符串为数组</p><p>D. 去掉字符串两侧指定字符</p></li></ol><ul><li><p>&ldquo;去掉字符串两侧指定字符"对应功能是 .strip()</p></li><li><p>&ldquo;按照指定字符分割字符串为数组"对应功能是 .split()</p></li><li><p>&ldquo;替换字符串中特定字符"对应功能是 .replace()</p></li><li><p>&ldquo;连接两个字符串序列"对应功能是 + 操作符</p></li></ul><ol start=3><li><p>字符串是一个连续的字符序列，哪个选项可以实现打印字符信息的换行？C</p><p>A. 使用“\换行”</p><p>B. 使用空格</p><p>C. 使用\n</p><p>D. 使用转义符\</p></li></ol><ul><li>换行符 \n，与 C 语言中一致。</li></ul><ol start=4><li><p>val=pow(2,1000)，请用一行代码返回val结果的长度值。D</p><p>A. len(val)</p><p>B. 其他答案均不正确</p><p>C. len(pow(2,1000))</p><p>D. len(str(val))</p></li></ol><ul><li>len(str(val)) 首先通过 str(val) 将数字类型 val 变成字符串，再通过 len() 获得字符串长度，即 val 的数字个数。</li></ul><ol start=5><li><p>下面关于 time 库引用不正确的是？C</p><p>A. from time import *</p><p>B. from time import strftime</p><p>C. from * import time</p><p>D. import time</p></li></ol><ul><li>from * import time 应该是： from time import *</li></ul><ol start=6><li><p>哪个选项不是 Python 语言的整数类型？D</p><p>A. 88</p><p>B. 0x9a</p><p>C. 0B1010</p><p>D. 0E99</p></li></ol><ul><li><p>十进制：一般表示</p></li><li><p>二进制：0b 或 0B 开头</p></li><li><p>八进制：0o 或 0O 开头</p></li><li><p>十六进制：0x 或 0X 开头</p></li><li><p>没有0E开头</p></li></ul><ol start=7><li><p>关于整数类型的 4 种进制表示，哪个选项的描述是正确的？C</p><p>A. 二进制、四进制、十进制、十六进制</p><p>B. 二进制、四进制、八进制、十六进制</p><p>C. 二进制、八进制、十进制、十六进制</p><p>D. 二进制、四进制、八进制、十进制</p></li></ol><ul><li>Python 不支持四进制整数</li></ul><ol start=8><li><p>哪个选项是 Python 语言 % 运算符的含义？D</p><p>A. x 与 y 之商</p><p>B. x 的 y 次幂</p><p>C. x 与 y 的整数商</p><p>D. x 与 y 之商的余数</p></li></ol><ul><li>% 是取余数，与 C 语言相同。</li></ul><ol start=9><li>哪个选项是下面代码的执行结果？A</li></ol><pre><code class=language-python>name=&quot;Python语言程序设计课程&quot;
print(name[0],name[2:-2],name[-1])
</code></pre><pre><code>A. P thon语言程序设计 程

B. P thon语言程序设计课 课

C. P thon语言程序设计课 程

D. P thon语言程序设计 课
</code></pre><ul><li>s[N:M] 表示对字符串 s 的切片操作，从 N 到 M，但不包含 M</li></ul><ol start=10><li>哪个选项是下面代码的执行结果？D</li></ol><pre><code class=language-python>s='PYTHON'
print(&quot;{0:3}&quot;.format(s))
</code></pre><pre><code>A. PYTHON

B. PYT

C. PYTH

D. PYTHON
</code></pre><ul><li>{0:3} 表示输出的宽度是 3，但如果字符串超过长度 3，则以字符串长度显示。</li></ul></article><div class=my-4><a href=https://b.hui.ke/tags/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/ class="inline-block bg-tertiary-bg text-sm rounded px-3 py-1 my-1 me-2 hover:text-eureka">#数据类型</a>
<a href=https://b.hui.ke/tags/time/ class="inline-block bg-tertiary-bg text-sm rounded px-3 py-1 my-1 me-2 hover:text-eureka">#time</a>
<a href=https://b.hui.ke/tags/%E6%96%87%E6%9C%AC%E8%BF%9B%E5%BA%A6%E6%9D%A1/ class="inline-block bg-tertiary-bg text-sm rounded px-3 py-1 my-1 me-2 hover:text-eureka">#文本进度条</a></div><div class=py-2><div class="my-8 flex flex-col items-center md:flex-row"><a href=https://b.hui.ke/authors/hui.ke/ class="md:me-4 text-primary-text h-24 w-24"><img src=https://b.hui.ke/bagua.webp class="bg-primary-bg w-full rounded-full" alt=Avatar></a><div class="mt-4 w-full md:mt-0 md:w-auto"><a href=https://b.hui.ke/authors/hui.ke/ class="mb-2 block border-b pb-1 text-lg font-bold"><h3>Hui.Ke</h3></a><span class="block pb-2">❤ Cyber Security | Safety is a priority.</span>
<a href=mailto:3199731997@qq.com class=me-2><i class="fas fa-envelope"></i></a>
<a href="https://wpa.qq.com/msgrd?v=3&uin=3199731997" class=me-2><i class="fab fa-qq"></i></a>
<a href=/images/aixinxianquan.webp class=me-2><i class="fab fa-weixin"></i></a></div></div></div><div class="-mx-2 mt-4 flex flex-col border-t px-2 pt-4 md:flex-row md:justify-between"><div><span class="text-primary-text block font-bold">Previous</span>
<a href=https://b.hui.ke/docs/python/13/ class=block>【第3周程序】基本数据类型</a></div><div class="mt-4 md:mt-0 md:text-right"><span class="text-primary-text block font-bold">Next</span>
<a href=https://b.hui.ke/docs/python/12/ class=block>【第2周程序】Python 基本图形绘制</a></div></div><div id=valine-comments class=mt-4></div><script defer src=https://cdn.jsdelivr.net/npm/valine@1.4.16/dist/Valine.min.js integrity=sha384-e0+DNUCJo75aOAzHQbFWYBCM9/S4f0BhRJXvEgbE3mMS85RM20MSSGStHuNdY2QK crossorigin></script>
<script>document.addEventListener("DOMContentLoaded",function(){new Valine({el:"#valine-comments",appId:"BQnVqWIiq78AdqwyhvBVAa3y-MdYXbMMI",appKey:"RKg5By312YjM8rU6WkkfK9IN",recordIP:"true",serverURLs:"https://l.hui.ke",visitor:"true"})})</script></div><div class="hidden lg:block lg:w-1/4"><div class="bg-secondary-bg
prose sticky top-16 z-10 hidden px-6 py-4 lg:block"><h3>On This Page</h3></div><div class="sticky-toc
border-s
hidden px-6 pb-6 lg:block"><nav id=TableOfContents><ul><li><a href=#notes>Notes</a><ul><li><a href=#整数类型>整数类型</a></li><li><a href=#浮点数类型>浮点数类型</a></li><li><a href=#复数类型>复数类型</a></li><li><a href=#数字类型的关系>数字类型的关系</a></li><li><a href=#字符串>字符串</a></li><li><a href=#unicode-编码>Unicode 编码</a></li><li><a href=#time-库>time 库</a><ul><li><a href=#时间获取>时间获取</a></li><li><a href=#时间格式化函数>时间格式化函数</a></li><li><a href=#程序计时>程序计时</a></li></ul></li></ul></li><li><a href=#测验-3>测验 3</a></li></ul></nav></div><script>window.addEventListener("DOMContentLoaded",()=>{enableStickyToc()})</script></div></div></div></div></div><script>document.addEventListener("DOMContentLoaded",()=>{hljs.highlightAll(),changeSidebarHeight(),switchDocToc()})</script></div></div></main><footer class=pl-scrollbar><div class="mx-auto w-full max-w-screen-xl"><div class="text-center p-6 pin-b"><script async src=/js/click.js></script><div id=poem_ip></div><script type=text/javascript>jinrishici.load(function(e){tags.innerHTML=e.data.matchTags})</script><div><span id=timeDate>载入年天数...</span><span id=times>载入时分秒...</span>
<script async src=/js/duration.js></script></div><a href=https://www.foreverblog.cn/go.html target=_blank><img src=https://img.foreverblog.cn/wormhole_4_tp.gif alt style=display:inline-block;width:auto;height:32px title=穿梭虫洞-随机访问十年之约友链博客></a><p class="text-sm text-tertiary-text"><script async src=//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js></script>本站总访问量 <span id=busuanzi_value_site_pv></span> 次
&#183; 您是本站的第 <span id=busuanzi_value_site_uv></span> 个小伙伴</p><script async src=/js/tab.js></script></div></div></footer></body></html>